package com.example.hrmsaas.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrmsaas.company.entity.Company;
import com.example.hrmsaas.company.mapper.CompanyMapper;
import com.example.hrmsaas.company.service.ICompanyService;
import com.example.hrmsaas.company.vo.CompanyVo;
import com.example.hrmsaas.system.utils.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 雷哥
 * @since 2021-10-28
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    private Log log = (Log) LogFactory.getLog(CompanyServiceImpl.class);

    @Autowired
    CompanyMapper companyMapper;
    @Transactional(readOnly = true)
    @Override
    public DataGridView getCompanyAll(CompanyVo companyVo) {
        QueryWrapper<Company> models = new QueryWrapper<>();
        if (null != companyVo) {
            models.eq(StringUtils.isNotBlank(companyVo.getName()), "name", companyVo.getName());

           // models.eq(companyVo.getId() != null, "id", companyVo.getId()).or().eq(companyVo.getId() != null, "pid", companyVo.getId());

        } else {
            log.info("MenuVo为空");
        }
        models.orderByDesc("id");
        Page<Company> page = new Page<>(companyVo.getPage(), companyVo.getLimit());
        this.companyMapper.selectPage(page, models);
        List<Company> list = new ArrayList<Company>();
        for (Company result : page.getRecords()) {
            list.add(result);
        }
        return new DataGridView(page.getTotal(), list);
    }

    @Override
    public DataGridView getCompanyOne(CompanyVo companyVo) {
        QueryWrapper<Company> models = new QueryWrapper<>();
        if (null != companyVo) {
            models.eq(companyVo.getId() != null, "id",companyVo.getId());
        } else {
            log.info("MenuVo为空");
        }
        Company data = companyMapper.selectOne(models);
        return new DataGridView(data);
    }
}
